package com.kingtown.inventory.adapter;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.kingtown.inventory.R;
import com.kingtown.inventory.domain.dto.SurplusDto;

import java.util.List;


public class SurplusListAdapter extends RecyclerView.Adapter<SurplusListAdapter.ViewHolder> implements View.OnClickListener{
    private List<SurplusDto> items;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View surplusView;
        TextView surplusCode,surplusTime;

        public ViewHolder(@NonNull View view) {
            super(view);
            surplusView = view;
            surplusCode = (TextView) view.findViewById(R.id.txt_surplus_code);
            surplusTime = (TextView) view.findViewById(R.id.txt_surplus_time);

        }
    }

    public SurplusListAdapter(List<SurplusDto> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.surplus_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SurplusDto item = items.get(position);
        Log.i("onBindViewHolder",item.toString());
        holder.surplusCode.setText(String.valueOf(item.getProductId()));
        holder.surplusTime.setText(item.getScanTime());
        holder.surplusView.setTag(item);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    private OnItemClickListener onItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onClick(v,  v.getTag());
        }
    }
}